package com.atguigu.bigdata.spark.sql

import org.apache.spark.SparkConf
import org.apache.spark.sql._
import org.apache.spark.sql.expressions.Aggregator

/**
  * create by undeRdoG on  2021-06-19  12:37
  * 凡心所向，素履以往，生如逆旅，一苇以航。
  */
object Spark04_SparkSQL_MySQL {

  def main(args: Array[String]): Unit = {
    val sparkConf = new SparkConf().setMaster("local[*]").setAppName("SparkSQL")

    val spark = SparkSession.builder().config(sparkConf).getOrCreate()

    import spark.implicits._

   /**
   *   读取 MySQL 数据源
   * */

    val df: DataFrame = spark.read
      .format("jdbc")
      .option("url", "jdbc:mysql://localhost:3306/bill")
      .option("driver", "com.mysql.jdbc.Driver")
      .option("user", "root")
      .option("password", "root")
      .option("dbtable", "bill")
      .load()
    df.show()

    df.write
      .format("jdbc")
      .option("url", "jdbc:mysql://localhost:3306/bill")
      .option("driver", "com.mysql.jdbc.Driver")
      .option("user", "root")
      .option("password", "root")
      .option("dbtable", "bill222")
      .mode(SaveMode.Append)
      .save()


  }

}
